﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using ArchivesAdmin.Util;
using ArchivesAdmin.Util.Model;
using ArchivesAdmin.Entity;
using ArchivesAdmin.Model;
using ArchivesAdmin.Admin.Web.Controllers;
using ArchivesAdmin.Entity.StorageManage;
using ArchivesAdmin.Business.StorageManage;
using ArchivesAdmin.Model.Param.StorageManage;
using System.Text;
using ArchivesAdmin.Service.StorageManage;
using ArchivesAdmin.Service;

namespace ArchivesAdmin.Admin.Web.Areas.StorageManage.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-19 09:36
    /// 描 述：控制器类   周转箱处理
    /// </summary>
    [Area("StorageManage")]
    public class CirculationController : BaseController
    {
        private BarLibBLL barLibBLL = new BarLibBLL();

        #region 视图功能

        //[AuthorizeFilter("storage:barlib:view")]
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        private string GetBoxCodesListNotMapped(TData<string> obj)
        {
            var sb = new StringBuilder();
            int count = 0;
            foreach (var item in obj.ResultList)
            {
                count++;
                sb.Append("<button type = 'button' class='btn btn-xs btn-info'>");
                sb.Append(item);
                sb.Append("</button>");
            }
            obj.TotalCount = count;

            return sb.ToString();
        }
        #region 获取数据
        //[HttpGet]
        //[AuthorizeFilter("storage:barlib:search")]
        public async Task<ActionResult> GetListJson(BarLibListParam param)
        {
            TData<List<BarLibEntity>> obj = await barLibBLL.GetList(param);
            return Json(obj);
        }

        //[HttpGet]
        //[AuthorizeFilter("storage:barlib:search")]
        public async Task<ActionResult> GetPageListJson(BarLibListParam param, Pagination pagination)
        {
            TData<List<BarLibEntity>> obj = await barLibBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<BarLibEntity> obj = await barLibBLL.GetEntity(id);
            return Json(obj);
        }
        #endregion

        #region 提交数据

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> ReprintCode(string code)
        {
            // 重打标签： 盒码 or  箱码
            TData<string> obj = await barLibBLL.ReprintCode(code);

            return Json(obj);
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> PrintBoxCode(BarLibPrintParam param)
        {
            // 打印盒码 --- for 档案周转 页面
            TData<string> obj = await barLibBLL.PrintBoxCode(param, "Circulation");
            string notmapped = GetBoxCodesListNotMapped(obj);

            obj.Description = notmapped;

            return Json(obj);
        }

        [HttpPost]
        //[AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> BindTempBinCode(BarLibPrintParam param)
        {
            // 打印箱码
            TData<string> obj = await barLibBLL.BindTempBinCode(param);
            return Json(obj);
        }

        // 批量打印
        [HttpPost]
        //  [AuthorizeFilter("storage:barlib:add,storage:barlib:edit")]
        public async Task<ActionResult> BatchPrintBoxCode(string boxCodes)
        {
            string[] str = boxCodes.Split('Z');
            string startId = "";

            BarLibService bs = new BarLibService();
            BarLibEntity entity = new BarLibEntity();
            foreach (string x in str)
            {
                if (x != "")
                {
                    entity = new BarLibEntity();
                    string childId = "";
                    string[] data = await bs.GetArchiveDataByCode(x.Insert(0, "Z"));
                    string allArchivesId = data[0];
                    string[] str1 = allArchivesId.Split(";");
                    startId = str1[0];
                    foreach (string p in str1)
                    {
                        if (!string.IsNullOrEmpty(p))
                            childId += await bs.GetArchiveChildIdByArchiveId(p) + "/";
                    }
                    entity.Boxcode = x.Insert(0, "Z");
                    entity.Archiveid = str1[str1.Length - 2];
                    entity.Isscreenchildid = int.Parse(data[1]);
                    PrinterHelper.PrintLabel(entity, startId, childId);
                }

            }
            TData<string> obj = new TData<string>();
            obj.Tag = 1;
            return Json(obj);
        }
        #endregion
    }
}
